using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace  EasyNetWork
{
    public class ReceiveState : IDisposable
    {
        private const int DefaultBufferLength = 1024 * 64;
        private MemoryStream m_Stream;
        private IPacketHeader m_PacketHeader;
        private bool m_Disposed;

        public ReceiveState()
        {
            m_Stream = new MemoryStream(DefaultBufferLength);
            m_PacketHeader = null;
            m_Disposed = false;
        }

        public MemoryStream Stream
        {
            get
            {
                return m_Stream;
            }
        }

        public IPacketHeader PacketHeader
        {
            get
            {
                return m_PacketHeader;
            }
        }

        public void PrepareForPacketHeader(int packetHeaderLength)
        {
            Reset(packetHeaderLength,null);
        }

        public void PrepareForPacket(IPacketHeader packetHeader)
        {
            if (packetHeader == null)
            {
                throw new System.Exception($"包头无效");
            }

            Reset(packetHeader.PacketLength, packetHeader);
        }

        public void Dispose()
        {
            
        }

        private void Dispose(bool disposing)
        {
            if (m_Disposed)
            {
                return;
            }

            if (disposing)
            {
                if (m_Stream != null)
                {
                    m_Stream.Dispose();
                    m_Stream = null;
                }
                
                
            }

            m_Disposed = true;
        }

        private void Reset(int targetLength,IPacketHeader packetHeader)
        {
            if (targetLength < 0)
            {
                throw new System.Exception("目标长度无效");
            }

            m_Stream.Position = 0L;
            m_Stream.SetLength(targetLength);
            m_PacketHeader = packetHeader;
        }
        
        
    }
}

